<template>
  <div class="dialog-form">
    <el-dialog v-model="formVisible" :title="pageTitle + '楼层'" @close="onClickCloseBtn">
      <div class="dialogCenter">
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
          <el-form-item label="楼层名称：" prop="name">
            <el-input v-model="formData.name" :maxlength="5" class="wt-400" clearable placeholder="请输入"
                      show-limit-number/>
          </el-form-item>
          <el-form-item label="排序：" prop="code">
            <el-input-number v-model="formData.code" :decimal-places="0" :max="maxNumber" :min="minNumber" large-number
                             @blur="textBlurNo" @change="textBlurNo"/>
          </el-form-item>
        </el-form>
        <div class="dialog-footer">
          <el-button @click="onClickCloseBtn">取 消</el-button>
          <el-button v-if="formData.id" :class="roomVoList.length > 0 ? 'bt-forbid' : ''" class="bt-red wt-60"
                     type="button" @click="handleDelete(formData.id)"><span>删除</span>
          </el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import useUserStore from '@/store/modules/user';

const {proxy} = getCurrentInstance();

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  roomVoList: {
    type: Array,
    default: () => {
      return [];
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  minNumber: {
    type: Number,
    default: 1,
  },
  maxNumber: {
    type: Number,
    default: 1,
  },
  title: {
    type: String,
    default: '新增',
  },
});

const emit = defineEmits([
  'handleClose',
  'fetchData',
  'handleAdd',
  'handleEdit',
  'handleDelete',
]);
const userStore = useUserStore();
const resetType = ref('empty');
const form = ref();
const formVisible = ref(false);

const formData = ref({
  code: 1,
});

const rules = {
  name: [
    {
      required: true,
      message: '楼层名称为空，请输入楼层名称',
      trigger: 'blur',
    },
  ],
  code: [
    {
      required: true,
      message: '排序为空，请输入排序',
      trigger: 'blur',
    },
  ],
};

const pageTitle = ref();

watch(
    () => props.visible,
    (val) => {
      formVisible.value = val;
      pageTitle.value = props.title;
    }
);

watch(
    () => props.data,
    (val) => {
      formData.value.id = val.id;
      formData.value.name = val.name;
      formData.value.code = val.code;
    }
);

// 提交表单
const onSubmit = () => {
  proxy.$refs['formRef'].validate((valid) => {
    if (valid) {
      if (props.title === '新增') {
        formData.value.id = '';
        emit('handleAdd', formData.value);
      } else {
        emit('handleEdit', formData.value);
      }
      userStore.setFloorInfo(JSON.parse(JSON.stringify(formData.value)));
    }
  });
};

// 清除表单数据
const handleClear = () => {
  proxy.resetForm('formRef');
  formData.value.code = 1;
};

// 点击取消关闭
const onClickCloseBtn = () => {
  handleClear();
  emit('handleClose');
};

// 监听排序
const textBlurNo = () => {
  const data = Number(formData.value.code);
  if (data <= 1) {
    formData.value.code = 1;
  }
  if (data > 99999999) {
    formData.value.code = 99999999;
  }
};

// 删除
const handleDelete = (id) => {
  if (props.roomVoList.length === 0) {
    emit('handleDelete', id);
  }
};

defineExpose({
  handleClear,
});
</script>
